package org.example.util;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

public class HttpUtils {
    private static final SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    static{
        clientHttpRequestFactory.setConnectTimeout(5000);
        clientHttpRequestFactory.setReadTimeout(5000);
    }
    private static final RestTemplate template = new RestTemplate(clientHttpRequestFactory);

    public static HttpHeaders getDefaultHeader() {
        HttpHeaders headers = new HttpHeaders();
        headers.set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.43");
        return headers;
    }

    public static Map<?, ?> get(String url) {
        return (Map<?, ?>) template.exchange(url, HttpMethod.GET, new HttpEntity<>(getDefaultHeader()), HashMap.class).getBody();
    }
}
